<?php

namespace app\common\business\news;

use app\common\business\BaseBus;
use app\common\model\News;

class NewsBus extends BaseBus
{
    /**
     * 资讯列表
     * @param $type
     * @param $page
     * @param $num
     * @return News[]|array|\think\Collection|\think\db\Query[]|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function newsListBus($type=0, $lang='en', $page = 1, $num = 10)
    {
        $where = [
            'status' => 1,
            'device_lang' => $lang
        ];
        if ($type > 0) {
            $where['type'] = $type;
        }
        $lists = News::field('id,title,abstract,image,level,post_hits,created_at')
            ->where($where)
            ->page($page, $num)
            ->order('id desc')
            ->select();
        $countTotal = News::where($where)->count();
        return ['count' => $countTotal, 'page' => $page, 'lists' => $lists];
    }

    /**
     * 资讯详情
     * @param $newId
     * @return array|mixed|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function newDetail($newId = 0)
    {
        if ($newId == 0) return [];
        $where = [
            'id' => $newId,
            'status' => 1
        ];
        $info = News::field('id,title,abstract,image,content,level,type,post_hits,created_at')
            ->where($where)
            ->find();
        if ($info) {
            $info->post_hits += 1;
            $info->save();
        }
        return $info;
    }
}
